package com.example.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.CaiwuService;

@Controller
@CrossOrigin  //允许跨域请求
public class LoginController {
	// 依赖注入
	@Autowired
	private CaiwuService service;

	// 登陆
		@RequestMapping(value="/login",method = RequestMethod.GET)
		@ResponseBody
		public JSON login(HttpServletResponse response,HttpServletRequest request) throws Exception {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
		    JSONObject json = new JSONObject();
			Map<String,Object> usermap = service.getuser(username, password);
			 if(null==usermap){
					json.put("retcode", "9999");
					json.put("retmsg","用户不存在或用户密码错误！");
					return json;
				}
			if(""!=usermap.get("username")|null!=usermap.get("username")|!"".equals(usermap.get("username"))) {
				 json.put("retcode", "0000");
				 json.put("retmsg", "查询成功！");
				 json.put("username", usermap.get("username"));
				return json;
			}   
			return json;
		}
}
